#include "ET++.ph"
#ifdef __GNUG__
#pragma implementation
#endif

#include "GotoDialog.h"

#include "Class.h"
#include "Dialog.h"
#include "Window.h"
#include "Fields.h"
#include "TextView.h"
#include "BorderItems.h"
#include "Buttons.h"
#include "Document.h"
#include "WindowSystem.h"
#include "ObjectTable.h"
#include "Box.h"
#include "Math.h"

static GotoDialog *gotoDialog;

//---- entry Point -------------------------------------------------------------

void GotoLine(TextView *tv)
{
	if (gotoDialog == 0)
		gotoDialog= new GotoDialog;
	gotoDialog->ShowGotoDialog(tv);
}

ONEXIT(GotoDialog)
{
	SafeDelete(gotoDialog);
}

//---- GotoDialog --------------------------------------------------------------

NewMetaImpl(GotoDialog, Dialog, (TP(view), TP(line), TP(gotoDialog)));

GotoDialog::GotoDialog() : Dialog("Goto Line")
{
	line= 0;
}

void GotoDialog::ShowGotoDialog(TextView *v)
{
	if (v) {
		view= v;
		if (ShowOnWindow(v->GetWindow()) == cIdYes)
			view->ShowLine(line->GetValue(), 500);
	}
}

VObject *GotoDialog::DoMakeControls()
{
	return
		new HBox(10, (VObjAlign)(eVObjVBase|eVObjHExpand),
			new TextItem("Line:"),
			line= new IntField(cIdNone),
			0
		);
}

VObject *GotoDialog::DoMakeActions()
{
	return
		new HBox(20, (VObjAlign)(eVObjVBase|eVObjHEqual|eVObjHExpand),
			new ActionButton(cIdYes,    "Goto", TRUE),
			new ActionButton(cIdCancel, "Cancel"),
			0
		);
}

void GotoDialog::DoSetDefaults()
{
	int f, t;
	view->GetSelection(&f, &t);
	line->SetValue(view->CharToLine(f)+1);
	line->SetRange(1, Math::Max(1, view->NumberOfLines()));
}

